/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.mlc.swing.layout.LayoutConstraintsManager;

/**
 * StartPulseOptionsPanel.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class StartPulseOptionsPanelHandler extends JPanel implements PanelHandler {

    private static final String START_PULSE_OPTIONS_PANEL_LAYOUT = "START_PULSE_CONFIGURATION_PANEL_LAYOUT.XML";
    private static final String[] PARALLEL_PORT_PINS = { "11", "12", "13" };
    private static final String[] START_PULSE_OPTIONS = { "Experiment", "Segment", "Trial" };

    private JLabel parallelPortSelectionLabel;
    private JLabel startPulseSelectionLabel;

    private JCheckBox externalPulseStartOptionCheckbox;

    private JComboBox startPulseToStartComboBox;
    private JComboBox parallelPortPinSelectionComboBox;

    private JPanel startPulseOptionsPanel;

    public StartPulseOptionsPanelHandler() {
	initComponents();
	addComponents();
    }

    /**
     * 
     */
    private void initComponents() {
	startPulseOptionsPanel = new JPanel();
	parallelPortSelectionLabel = new JLabel("Parallel Port PIN for start pulse");
	externalPulseStartOptionCheckbox = new JCheckBox("Start experiment with external pulse");
	startPulseSelectionLabel = new JLabel("Start Pulse to start");
	startPulseToStartComboBox = new JComboBox(START_PULSE_OPTIONS);
	parallelPortPinSelectionComboBox = new JComboBox(PARALLEL_PORT_PINS);
    }

    /**
     * 
     */
    private void addComponents() {

	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(START_PULSE_OPTIONS_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	startPulseOptionsPanel.setLayout(panelLayout);

	// here we add the controls to the container.
	startPulseOptionsPanel.add(parallelPortSelectionLabel, "parallelPortSelectionLabel");
	startPulseOptionsPanel.add(externalPulseStartOptionCheckbox, "externalPulseStartOptionCheckbox");
	startPulseOptionsPanel.add(startPulseSelectionLabel, "startPulseSelectionLabel");
	startPulseOptionsPanel.add(startPulseToStartComboBox, "startPulseToStartComboBox");
	startPulseOptionsPanel.add(parallelPortPinSelectionComboBox, "parallelPortPinSelectionComboBox");
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
	// TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * 
     * @see slhs.tempo.scriptGenerator.PanelHandler#getPanel()
     */
    @Override
    public JPanel getPanel() {
	// TODO Auto-generated method stub
	return startPulseOptionsPanel;
    }

    private String doesStartWithExternalPulse() {
	boolean status = externalPulseStartOptionCheckbox.isSelected();
	return ((status) ? UtilityConstants.TRUE : UtilityConstants.FALSE);
    }

    private String getParallelPortPin() {
	return parallelPortPinSelectionComboBox.getSelectedItem().toString();
    }

    private String getStartPulseToStart() {
	return Integer.toString(startPulseToStartComboBox.getSelectedIndex());
    }

    /**
     * These values are not explained in the template. For now hardcoding them
     * */
    private String getOtherStartPulseOptions() {
	return "0;True;False";
    }

    /**
     * @return
     */
    public String getStartPulseOptions() {
	StringBuffer startPulseOptionsString = new StringBuffer();

	startPulseOptionsString.append(getOtherStartPulseOptions());
	startPulseOptionsString.append(UtilityConstants.VALUES_DELIMITER);

	startPulseOptionsString.append(doesStartWithExternalPulse());
	startPulseOptionsString.append(UtilityConstants.VALUES_DELIMITER);

	startPulseOptionsString.append(getParallelPortPin());
	startPulseOptionsString.append(UtilityConstants.VALUES_DELIMITER);

	startPulseOptionsString.append(getStartPulseToStart());

	return startPulseOptionsString.toString();
    }

}
